package flc.ast.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ConvertUtils;
import com.bumptech.glide.Glide;
import com.stark.picselect.entity.SelectMediaEntity;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import stark.common.basic.utils.TimeUtil;

public class PlayListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<SelectMediaEntity> mDatas = new ArrayList<>();//数据
    private final Context mContext;

    public PlayListAdapter(List<SelectMediaEntity> mDatas, Context mContext) {
        this.mDatas = mDatas;
        this.mContext = mContext;
    }

    public PlayListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setmDatas(List<SelectMediaEntity> mDatas) {
        this.mDatas = mDatas;
    }

    //第一步 定义接口
    public interface OnItemClickListener {
        void onClick(SelectMediaEntity bean, int position);

        void onClickDelete(SelectMediaEntity bean, int position);
    }

    private OnItemClickListener mListener;

    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemHolder(LayoutInflater.from(mContext).inflate(R.layout.item_play_list, parent, false));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {

        if (holder instanceof ItemHolder) {
            Glide.with(holder.itemView).load(mDatas.get(position).getPath()).into(((ItemHolder) holder).ivImg);
            ((ItemHolder) holder).tvName.setText(mDatas.get(position).getMediaName());
            ((ItemHolder) holder).tvSize.setText(ConvertUtils.byte2FitMemorySize(mDatas.get(position).getSize(), 0));
            ((ItemHolder) holder).tvDuration.setText(TimeUtil.getMmss(mDatas.get(position).getDuration()));

            if (mDatas.get(position).isChecked()) {
                ((ItemHolder) holder).llItem.setBackgroundColor(Color.parseColor("#1A2D53FF"));
            } else {
                ((ItemHolder) holder).llItem.setBackgroundColor(0);
            }

            ((ItemHolder) holder).tvDelete.setOnClickListener(v ->
                    mListener.onClickDelete(mDatas.get(position), position));
            holder.itemView.setOnClickListener(v ->
                    mListener.onClick(mDatas.get(position), position));
        } else {

        }
    }

    //多出尾部刷新的item
    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    //item的holder
    static class ItemHolder extends RecyclerView.ViewHolder {
        private final LinearLayout llItem;
        private final TextView tvName, tvSize, tvDuration, tvDelete;
        private final ImageView ivImg;

        public ItemHolder(View itemView) {
            super(itemView);

            llItem = itemView.findViewById(R.id.llPlayListItem);

            tvName = itemView.findViewById(R.id.tvPlayListItemName);
            tvSize = itemView.findViewById(R.id.tvPlayListItemSize);
            tvDuration = itemView.findViewById(R.id.tvPlayListItemDuration);
            tvDelete = itemView.findViewById(R.id.tvPlayListItemDelete);

            ivImg = itemView.findViewById(R.id.ivPlayListItemImg);
        }
    }

}

